package com.CST.service.impl;

import com.CST.entity.BdAccount;
import com.CST.mapper.dataplatform.BdAccountMapper;
import com.CST.service.IBdAccountService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ly
 * @since 2021-12-08
 */
@Service
public class BdAccountServiceImpl extends ServiceImpl<BdAccountMapper, BdAccount> implements IBdAccountService {


    @Override
    public List<BdAccount> getAccountSource(BdAccount entity) {
        // 查询所有成本数据
        return baseMapper.getAccountSource(entity);

    }

    @Override
    public List<BdAccount> getAccountOrgationSource(BdAccount entity) {
        // 查询所有成本数据
        List<BdAccount> accountList = baseMapper.getAccountOrgationSource(entity);
        accountList.forEach(item -> {
            if (item.getAccountcode().length() > 4) {
                int len = item.getAccountcode().length();
                item.setParentAccountCode(item.getAccountcode().substring(0, len - 2));
            } else {
                item.setParentAccountCode("0");
            }
        });

        List<BdAccount> bList = accountList.stream().map(item -> {
            BdAccount info = new BdAccount();
            BeanUtils.copyProperties(item, info);
            return info;
        }).collect(Collectors.toList());

        // 存放树结构的数据
        List<BdAccount> costTreeList = new ArrayList<>();
        if (bList.size() > 0 && bList != null) {
            // 找到所有的一级节点存放在一个list中
            List<BdAccount> parntList = new ArrayList<>();
            for (BdAccount pList : bList) {
                // 父节点的pID为 1，这边按自己实际情况来判断
                if ("1".equals(pList.getParentcode())) {
                    parntList.add(pList);
                }
//                if(!"1".equals(pList.getIsend()))
//                {
//                    parntList.add(pList);
//                }
            }
            // 为一级节点设置子节点 ,getChildren采用递归算法
            for (BdAccount amp : parntList) {
                BdAccount info = new BdAccount();
                info = amp;
                info.setChildrenList(getChildren(amp.getAccountcode(), bList));
                costTreeList.add(amp);
            }
        }
        return costTreeList;
    }

    @Override
    public List<BdAccount> getCheck(BdAccount entity) {
        // 查询所有成本数据
        List<BdAccount> list = new ArrayList<>();
        if(entity.getSysid().equals("Cost"))
        {
            entity.setEdParentCode("RD_CostUFIDARelation");
        }
        if(entity.getSysid().equals("Income"))
        {
            entity.setEdParentCode("RD_IncomeUFIDARelation");
        }
        list = baseMapper.getAccountOrgationSource(entity);
        if(CollectionUtils.isNotEmpty(list))
        {
            list =list.stream().filter(t->t.getIsend()== 1).distinct().collect(Collectors.toList());
        }
        return list;
    }


    /**
     * 递归组装树节点数据
     *
     * @param accountCode 父节点
     * @param list        所有数据
     * @return
     */
    public List<BdAccount> getChildren(String accountCode, List<BdAccount> list) {
        List<BdAccount> childrenList = new ArrayList<>();
        // 子节点
        for (BdAccount children : list) {
            if (accountCode.equals(children.getParentAccountCode())) {
                childrenList.add(children);
            }
        }
        // 把子节点的子节点循环一遍
        for (BdAccount cList : childrenList) {
            cList.setChildrenList(getChildren(cList.getAccountcode(), list));
        }
        return childrenList;
    }

}
